package com.sk.sourcecircle.dialog;

import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.callback.MyCallBack;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/11/19.
 */

public class ReleaseDialogFragment extends DialogFragment implements WeatherSearch.OnWeatherSearchListener {

    @BindView(R.id.date_day)
    TextView dateDay;
    @BindView(R.id.date_week)
    TextView dateWeek;
    @BindView(R.id.date_month)
    TextView dateMonth;
    @BindView(R.id.tv_weather)
    TextView tvWeather;
    @BindView(R.id.city_ll)
    LinearLayout cityLl;
    @BindView(R.id.release_photo)
    TextView releasePhoto;
    @BindView(R.id.join)
    TextView join;
    @BindView(R.id.close)
    ImageView close;
    Unbinder unbinder;

    private MyCallBack myCallBack;

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int style = android.support.v4.app.DialogFragment.STYLE_NO_TITLE, theme = 0;
        setStyle(style, theme);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_release, container, false);

        unbinder = ButterKnife.bind(this, view);


        WeatherSearchQuery mquery = new WeatherSearchQuery(MyApplication.getInstance().getCityName(), WeatherSearchQuery.WEATHER_TYPE_LIVE);
        WeatherSearch mweathersearch = new WeatherSearch(getActivity());
        mweathersearch.setOnWeatherSearchListener(this);
        mweathersearch.setQuery(mquery);
        mweathersearch.searchWeatherAsyn(); //异步搜索
        return view;
    }

    public void onStart() {
        super.onStart();
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.release_photo, R.id.join, R.id.close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.release_photo:
                myCallBack.releaseAlbum();
                dismiss();
                break;
            case R.id.join:
                myCallBack.joinGroup();
                dismiss();
                break;
            case R.id.close:
                dismiss();
                break;
        }
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }


    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int rCode) {
        if (rCode == 1000) {
            if (localWeatherLiveResult != null && localWeatherLiveResult.getLiveResult() != null) {
                LocalWeatherLive weatherlive = localWeatherLiveResult.getLiveResult();
                Calendar calendar = Calendar.getInstance();  //获取当前时间，作为图标的名字
                String year = calendar.get(Calendar.YEAR) + "";
                String month = calendar.get(Calendar.MONTH) + 1 + "";
                String day = calendar.get(Calendar.DAY_OF_MONTH) + "";
                int week = calendar.get(Calendar.DAY_OF_WEEK);
                dateMonth.setText(month + "/" + year);
                dateDay.setText(day);
                switch (week) {
                    case 1:
                        dateWeek.setText("星期日");
                        break;
                    case 2:
                        dateWeek.setText("星期一");
                        break;
                    case 3:
                        dateWeek.setText("星期二");
                        break;
                    case 4:
                        dateWeek.setText("星期三");
                        break;
                    case 5:
                        dateWeek.setText("星期四");
                        break;
                    case 6:
                        dateWeek.setText("星期五");
                        break;
                    case 7:
                        dateWeek.setText("星期六");
                        break;
                }


                tvWeather.setText(MyApplication.getInstance().getCityName() + ": " + weatherlive.getWeather() + " " + weatherlive.getTemperature() + "℃。" + "湿度 " + weatherlive.getHumidity() + "%\n" + weatherlive.getWindDirection() + "风     " + weatherlive.getWindPower() + "级");
            } else {
                Toast.makeText(getActivity(), "天气数据获取失败！", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getActivity(), "天气数据获取失败！", Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {

    }
}
